// 状态
import {
	getBaseCategoryList,
	getFloorList,
	getGoodsList,
	getLikeList, getProductInfo,
	getRankList,
	postSearchGoodsList
} from "@/api/product";

const state = {
	// 三级分类数据
	categoryList: [],
	// 商品排行
	rankList: [],
	// 商品排行2
	goodsList: [],
	// 猜你喜欢的商品数据
	likeList: [],
	// 猜你喜欢的商品数据的总页数
	likePageSum: 1,
	// 楼层数据
	floorList: [],
	// 搜索出来的商品
	searchResult: {
		pageNo: 1,// 初始页码
		pageSize: 5,// 每页显示的数量
		total: 1,// 总条数
	},
	// 商品的详情
	productInfo: {
		// 面包屑导航相关数据
		categoryView: {},
		// 商品详情
		skuInfo: {}
	},
	cartInfoSuccess:{}
	
};
const getters = {
	zoomImgUrl(state) {
		return state.productInfo.skuInfo.skuImageList?.find(v => v.isDefault === "1").imgUrl;
	}
}
const mutations = {
	UP_CARTINFO_SUCCESS(state,payload){
		// 商品的ID
		// 商品的图片
		// 商品的标题
		// 商品的配置
		// 购买的数量
		state.cartInfoSuccess = payload;
	},
	// 切换配置
	UP_spuSaleAttrList_CHECKED(state, {id1, id2}) {
		const {spuSaleAttrValueList} = state.productInfo.spuSaleAttrList.find(v => v.id === id1);
		// 之前选中的
		spuSaleAttrValueList.find(v => v.isChecked === "1").isChecked = "0";
		// 将选中的配件进行高亮显示
		spuSaleAttrValueList.find(v => v.id === id2).isChecked = "1";
		
	},
	// 更改skuInfo->skuImageList->isDefault的值
	UP_SKUIMAGELIST_ISDEFAULT(state, id) {
		// 获取到之前选中的图片
		const checkedInfo = state.productInfo.skuInfo.skuImageList.find(v => v.isDefault === "1");
		// 将之前选中的图片isDefault设置为0（取消高亮）
		checkedInfo.isDefault = "0";
		// 获取当前点击的图片
		const info = state.productInfo.skuInfo.skuImageList.find(v => v.id === id);
		// 让当前点击的图片高亮。
		info.isDefault = "1";
		state.productInfo.skuInfo.skuDefaultImg = info.imgUrl;
		
		// state.productInfo.skuInfo.skuImageList.forEach(info=>{
		// 	if(info.id === id) info.isDefault = "1";
		// 	else info.isDefault = "0";
		// })
		
		// let num = 0;
		// for(let i=0;i<state.productInfo.skuInfo.skuImageList.length;i++){
		// 	if(info.id === id) {
		// 		info.isDefault = "1";
		// 		num++;
		// 	}
		// 	else if(info.isDefault === "1"){
		// 		info.isDefault = 0;
		// 		num++;
		// 	}
		// 	if(num===2) break;
		// }
		
		
	},
	// 更新商品详情
	UP_PRODUCT_INFO(state, payload) {
		state.productInfo = payload;
	},
	CHANGE_ACTIVEINDEX_BYID(state, payload) {
		// payload-->{id:条件,activeIndex:修改的值}
		const info = state.floorList.find(v => v.id === payload.id);
		info.activeIndex = payload.activeIndex;
	},
	// 更新三级分类数据
	UP_CATEGORY_LIST(state, payload) {
		state.categoryList = payload;
	},
	UP_RANKLIST(state, payload) {
		state.rankList = payload;
	},
	UP_GOODSLIST(state, payload) {
		state.goodsList = payload;
	},
	UP_LIKELIST_AND_PAGESUM(state, payload) {
		state.likeList = payload.likeList;
		state.likePageSum = payload.pageSum;
	},
	UP_FLOORLIST(state, payload) {
		state.floorList = payload.map(item => {
			item.activeIndex = 0;
			return item;
		});
	},
	UP_SEARCH_RESULT(state, payload) {
		state.searchResult = payload;
	}
};
// action:异步行为
const actions = {
	// 根据ID获取商品详情
	async getProductInfoAsync({commit}, id) {
		const res = await getProductInfo(id);
		commit("UP_PRODUCT_INFO", res.data);
	},
	// 搜索
	async postSearchGoodsListAsync({commit}, body) {
		const res = await postSearchGoodsList(body);
		commit("UP_SEARCH_RESULT", res.data);
	},
	
	// 获取三级数据分类
	async getBaseCategoryListAsync({commit, state}, limit = 15) {
		if (state.categoryList.length > 0) return;
		const res = await getBaseCategoryList();
		commit("UP_CATEGORY_LIST", res.data.slice(0, limit));
	},
	// 获取商品排行
	async getRankListAsync({commit}) {
		const res = await getRankList();
		commit("UP_RANKLIST", res.rankList);
		
	},
	// 获取商品排行 2
	async getGoodsListAsync({commit}, type = 1) {
		const res = await getGoodsList(type);
		commit("UP_GOODSLIST", res.goodsList)
	},
	// 获取猜你喜欢
	async getLikeListAsync({commit}, {pageNo, pageSize = 6}) {
		const res = await getLikeList(pageNo, pageSize);
		commit("UP_LIKELIST_AND_PAGESUM", res);
	},
	async getFloorListAsync({commit}) {
		const res = await getFloorList();
		commit("UP_FLOORLIST", res.floorList);
	},
	// 根据ID获取到商品详情，并对数据进行二次过滤
	async getCartInfoSuccess({commit,dispatch,state},{id,buyNum=1,saleAttrStr}){
		// 1- 根据ID获取到商品详情
		await dispatch("getProductInfoAsync",id);
		// 判断自定义的配置是否存在
		if(!saleAttrStr){
			saleAttrStr=state.productInfo.skuInfo.skuSaleAttrValueList.map(item=>item.saleAttrName+":"+item.saleAttrValueName).join("|");
		}
		// console.log(state.productInfo);
		// console.log("id",id);// 商品ID
		// console.log("skuName",state.productInfo.skuInfo.skuName);// 商品名字
		// console.log("skuDefaultImg",state.productInfo.skuInfo.skuDefaultImg);// 商品图片
		// console.log("saleAttrStr",saleAttrStr);// 商品配置
		// console.log("buyNum",buyNum);// 购买数量
		// 2- 根据商品详情获取到加入购物车成功的提示信息
		commit("UP_CARTINFO_SUCCESS",{
			id,
			skuName:state.productInfo.skuInfo.skuName,
			skuDefaultImg:state.productInfo.skuInfo.skuDefaultImg,
			saleAttrStr:saleAttrStr,
			buyNum
		})
	}
}
export default {
	namespaced: true,
	getters,
	state,
	actions,
	mutations
}